package com.ankit.GPS;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class LocateMeActivity extends Activity {
	LocationManager mLocationManager;
	TextView tv;
	Location mLocation;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tv = (TextView) findViewById(R.id.tv1);
		
		mLocationManager = (LocationManager)
		getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		
		mLocationManager.getBestProvider(criteria,true);
		
		List<String> providers = mLocationManager.getProviders(true);
		StringBuilder mSB = new StringBuilder("Providers:\n");
		
		for(int i = 0; i<providers.size(); i++) {
			mLocationManager.requestLocationUpdates(
			providers.get(i), 5000, 0, new LocationListener(){
				// these methods are required
				public void onLocationChanged(Location location) {}
				public void onProviderDisabled(String arg0) {}
				public void onProviderEnabled(String provider) {}
				public void onStatusChanged(String a, int b, Bundle c) {}
			});
			
			mSB.append(providers.get(i)).append(": \n");
			mLocation = mLocationManager.getLastKnownLocation(providers.get(i));
			if(mLocation != null) {
				mSB.append(mLocation.getLatitude()).append(" , ");
				mSB.append(mLocation.getLongitude()).append("\n");
			} 
			else 
			{
			    mSB.append("Location can not be found");
			}
		}
		tv.setText(mSB.toString());
	}
}